/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *******************************************************************************/
package com.jhx.common.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jhx.common.R;
import com.jhx.common.widget.ViewPagerFixed;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import cn.finalteam.rxgalleryfinal.bean.MediaBean;

import uk.co.senab.photoview.PhotoView;

/**
 * 图片预览
 *
 * @author
 */
public class PicturePreviewActivity extends AppCompatActivity {
    public static final String KEY_DATA = "data";
	public static final String KEY_INDEX = "index";
	public static final String KEY_LIST = "list";

	private List<String> urlList;
	private List<MediaBean> mBeanList = new ArrayList<>();
    private int index = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
		ViewPagerFixed viewPager = findViewById(R.id.view_pager);
		TextView titleText = findViewById(R.id.toolbar_title);
		Toolbar toolbar = findViewById(R.id.toolbar);
		titleText.setText("图片预览");
		toolbar.setNavigationIcon(R.mipmap.icon_back_white);
		toolbar.setNavigationOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
                finish();
			}
		});
		Intent intent = getIntent();
		if(intent != null) {
			urlList = intent.getStringArrayListExtra(KEY_DATA);
			index = intent.getIntExtra(KEY_INDEX, 0);
			mBeanList = intent.getParcelableArrayListExtra(KEY_LIST);
		}
		if(urlList == null) {
            urlList = new ArrayList<>();
        }
		if(mBeanList != null && mBeanList.size() > 0) {
			for(MediaBean mediaBean : mBeanList) {
				if(!TextUtils.isEmpty(mediaBean.getOriginalPath())) {
					urlList.add(mediaBean.getOriginalPath());
				}
			}
		}
		viewPager.setAdapter(new SamplePagerAdapter());
		viewPager.setCurrentItem(index < urlList.size()? index : 0);
	}

	private class SamplePagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return urlList.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			String path = urlList.get(position);
			if(path.toUpperCase(Locale.US).startsWith("HTTP://")) {
				Glide.with(PicturePreviewActivity.this).load(path).error(R.mipmap.img_default).into(photoView);
			} else {
				File file = new File(path);
				Glide.with(PicturePreviewActivity.this).load(file).error(R.mipmap.img_default).into(photoView);
			}
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}
}
